//
//  AdvertisingDetailViewModel.swift
//  Exchange
//
//  Created by rui on 2018/6/13.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit


class AdvertisingDetailViewModel: BaseViewModel {
    
    ///币种
    var dataModel: AdvertisingListModel?
    
    ///单价
    var unitPrice: Double = 0.0
    ///金额
    var amount: Double = 0.0
    ///数量
    var quantity: Double = 0.0
    
    var orderId: Int?
    
    var adId: Int = 0
    
    ///创建广告会话，生成的会话id
    var adChatId: Int = 0
    
    private lazy var advertisingApi: AdvertisingDetailRequest = {
        let api = AdvertisingDetailRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    private lazy var createOrderApi: CreateTradeOrderRequest = {
        let api = CreateTradeOrderRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    private lazy var adWaitOrderApi: AdChatRequest = {
        let api = AdChatRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()

    func getAdvertisingDetail(adId: Int) {
        self.adId = adId
        self.advertisingApi.loadData()
    }
    
    func createTradeOrder(unitPrice: Double, amount: Double, quantity: Double,adId: Int) {
        self.adId = adId
        self.unitPrice = unitPrice
        self.amount = amount
        self.quantity = quantity
        self.createOrderApi.loadData()
    }
    
    func createAdChat(adId: Int) {
        self.adId = adId
        self.adWaitOrderApi.loadData()
    }
}

extension AdvertisingDetailViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if let _ = request as? AdvertisingDetailRequest {
            var flag = false
            if let data = data?["data"] as? [String:Any] {
                dPrint(data)
                if let jsonData = data.jsonData {
                    do{
                        let model = try JSONDecoder().decode(AdvertisingListModel.self, from: jsonData)
                        self.dataModel = model
                        flag = true
                    }catch {
                        flag = false
                        dPrint(error)
                    }
                }
            }
            self.response?(ResponseState.trade_ad_detail,flag, nil)
        }else if let _ = request as? CreateTradeOrderRequest {
            if let reData = data?["data"] as? [String:Any],let orderId = reData["id"] as? Int {
                self.orderId = orderId
            }
            self.response?(ResponseState.trade_order_create,true, nil)
        }else if let _ = request as? AdChatRequest {
            if let reData = data?["data"] as? [String:Any],let adChatId = reData["Id"] as? Int {
                self.adChatId = adChatId
            }
            self.response?(ResponseState.trade_ad_chat,true, nil)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? AdvertisingDetailRequest {
            self.response?(ResponseState.trade_ad_detail,false, request.errorMsg)
        }else if let _ = request as? CreateTradeOrderRequest {
            self.response?(ResponseState.trade_order_create,false, request.errorMsg)
        }else if let _ = request as? AdChatRequest {
            self.response?(ResponseState.trade_ad_chat,false, request.errorMsg)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? AdvertisingDetailRequest {
            pramas.updateValue(self.adId, forKey: kID)
        }else if let _ = request as? CreateTradeOrderRequest {
            pramas.updateValue(self.unitPrice, forKey: "unitPrice")
            pramas.updateValue(self.amount, forKey: "amount")
            pramas.updateValue(self.quantity, forKey: "quantity")
            pramas.updateValue(self.adId, forKey: "advertisingId")
        }else if let _ = request as? AdChatRequest {
            pramas.updateValue(self.adId, forKey: kID)
        }
        return pramas
    }
    
}
